﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TMScore_Subsets
{
    public class UsersManager // keeps all the users
    {
        public List<User> usersList; // list of all the users
        public UsersManager()
        {
            usersList = new List<User>();
        }

        /// <summary>
        /// this func send the parametrs for the TMscore calculation
        /// </summary>
        /// <param name="u"></param>
        /// <param name="time"></param>
        public void calculateTMscore(User u, DateTime time)
        {
            double tmscore =0;
            KeyValuePair<DateTime, double> pair;
            SubsetsTMScoreGenerator sg = new SubsetsTMScoreGenerator();
            tmscore = sg.GetTMScore(u.documentsList);
            pair = new KeyValuePair<DateTime, double>(time, Math.Round(tmscore, 5, MidpointRounding.AwayFromZero));
          
            u.subsetTMscore.Add(pair);

        }

        /// <summary>
        /// this func create a user from the recivers of the mail, if the user  exists already add the doc to his documents list add calculate the subsets 
        /// </summary>
        /// <param name="d"></param>
        public void CreateUser(Document d)
        {
            bool exist = false;
            bool found = false;
            KeyValuePair<DateTime, double> pair;
            foreach (string r in d.receivers)
            {
                exist = false;
                found = false;
                foreach (User u in usersList)
                {
                    if (r == u.m_name) // already exists
                    {
                        foreach (Document m in u.documentsList)
                        {
                            if (d.m_Name == m.m_Name)
                                exist = true;
                        }
                        if (exist == false&& d.m_DocumentContent.Length>0 && d.Fingerprints.Count>0)
                        {
                            u.documentsList.Add(d); // add the doc to the list
                            calculateTMscore(u, d.time);
                            found = true;
                        }
                      
                    }
                }
                if (!found)
                {
                    User user = new User(r);
                    usersList.Add(user); // add the doc to the list
                    pair = new KeyValuePair<DateTime, double>(d.time, d.TMScore); // the first document of the user .the subset will recieve the TMscore of the first doc. 
                    user.subsetTMscore.Add(pair);
                    user.documentsList.Add(d);
                }
            }
        }

        public static void WritetoFile(List<User> usersList)
        {
            // create a writer and open the file
            TextWriter tw = new StreamWriter("TMscore.txt");
            foreach (User u in usersList)
            {  
                // write a line of text to the file
                tw.WriteLine(u.m_name);
            
                foreach (KeyValuePair<DateTime, double> pair in u.subsetTMscore)
                {
                tw.Write("#"+ pair.Key +","+ pair.Value +" "+ "# ");
                   
                }
                tw.WriteLine(" ");
                // close the stream
            }
            tw.Close();
        }
        /// <summary>
        /// write into csv file the users and the count of their mails
        /// </summary>
        /// <param name="usersList"></param>
        public static void CreateCSVFile(List<User> usersList)
        {
            using (CsvFileWriter writer = new CsvFileWriter("users-data.csv"))
            {
               

                foreach (User u in usersList)
                {
                    CsvRow row = new CsvRow();
                    row.Add(String.Format("{0}", u.m_name));
                    row.Add(String.Format("{0}", u.subsetTMscore.Count));
                    writer.WriteRow(row);
                    
                }
           
               
                

              
            }
        }

    }
}

    
